package listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import jms.JmsMessageConsumer;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import data.VersionTable;
import util.PropertiesUtils;
import action.Action;
import action.impl.MessageAction;


public class JMSListener implements ServletContextListener {

	private final static Logger logger = LogManager.getLogger(JMSListener.class);
	
	private JmsMessageConsumer consumer;

    public JMSListener() {
    	consumer = new JmsMessageConsumer();
    }

    public void contextInitialized(ServletContextEvent sce) {
    	String imageSavePath = "image.save.path";
    	String imageGetPath = "image.get.path";
    	Action action = new MessageAction(PropertiesUtils.getProperties(imageSavePath));
    	sce.getServletContext().setAttribute("imagepath", PropertiesUtils.getProperties(imageGetPath));
    	consumer.setAction(action);
    	consumer.consume();
    	logger.info("jms consumer start");
    	
    	long time = System.currentTimeMillis();
    	VersionTable versionTable = VersionTable.getInstance();
    	versionTable.put(VersionTable.AUDIT_VERSION, time);
    	versionTable.put(VersionTable.STATUS_CHANGE__VERSION, time);
    	logger.info("verion table init");
    }

    public void contextDestroyed(ServletContextEvent sce) {
    	consumer.close();
    	logger.info("jms consumer destory");
    }
	
}
